//
//  LocationTransfer.swift
//  SYGovernment
//
//  Created by 思域 on 2020/9/11.
//  Copyright © 2020 思域. All rights reserved.
//

import Foundation
import CoreLocation

/// 坐标转换
public struct LocationConverter {

    static private let a = 6378245.0
    static private let ee = 0.00669342162296594323;
    static private let pi = 3.14159265358979324;
    static private let xPi = Double.pi * 3000.0 / 180.0;

    static public func convertFromWGSToGCJ(_ wgsLoc: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        var adjustLat = convertLatWithX(wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
        var adjustLon = convertLonWithX(wgsLoc.longitude - 105.0, y: wgsLoc.latitude - 35.0)
        let radLat = wgsLoc.latitude / 180.0 * pi;
        var magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        let sqrtMagic = sqrt(magic);
        adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
        return CLLocationCoordinate2D(latitude: wgsLoc.latitude + adjustLat, longitude: wgsLoc.longitude + adjustLon)
    }

    static public func convertFromGCJToWGS(_ p: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        let threshold = 0.00001;

        // The boundary
        var minLat = p.latitude - 0.5;
        var maxLat = p.latitude + 0.5;
        var minLng = p.longitude - 0.5;
        var maxLng = p.longitude + 0.5;

        var delta: Double = 1;
        var maxIteration = 30;
        // Binary search
        while (true) {
            let leftBottom = convertFromWGSToGCJ(CLLocationCoordinate2D(latitude: minLat, longitude: minLng))
            let rightBottom = convertFromWGSToGCJ(CLLocationCoordinate2D(latitude: minLat, longitude: maxLng))
            let leftUp = convertFromWGSToGCJ(CLLocationCoordinate2D(latitude: maxLat, longitude: minLng))
            let midPoint = convertFromWGSToGCJ(CLLocationCoordinate2D(latitude: ((minLat + maxLat) / 2), longitude: ((minLng + maxLng) / 2)))
            delta = fabs(midPoint.latitude - p.latitude) + fabs(midPoint.longitude - p.longitude);
            maxIteration -= 1
            if (maxIteration <= 0 || delta <= threshold) {
                return CLLocationCoordinate2D(latitude: ((minLat + maxLat) / 2), longitude: ((minLng + maxLng) / 2))
            }

            if (isContains(p, p1: leftBottom, p2: midPoint)) {
                maxLat = (minLat + maxLat) / 2;
                maxLng = (minLng + maxLng) / 2;
            } else if (isContains(p, p1: rightBottom, p2: midPoint)) {
                maxLat = (minLat + maxLat) / 2;
                minLng = (minLng + maxLng) / 2;
            } else if (isContains(p, p1: leftUp, p2: midPoint)) {
                minLat = (minLat + maxLat) / 2;
                maxLng = (minLng + maxLng) / 2;
            } else {
                minLat = (minLat + maxLat) / 2;
                minLng = (minLng + maxLng) / 2;
            }
        }
    }

    static public func convertFromGCJToBaidu(_ p: CLLocationCoordinate2D) -> CLLocationCoordinate2D {
        let z = sqrt(p.longitude * p.longitude + p.latitude * p.latitude) + 0.00002 * sin(p.latitude * xPi);
        let theta = atan2(p.latitude, p.longitude) + 0.000003 * cos(p.longitude * xPi);
        return CLLocationCoordinate2D(latitude: (z * sin(theta) + 0.006), longitude: (z * cos(theta) + 0.0065));
    }

    static public func convertFromBaiduToGCJ(_ p: CLLocationCoordinate2D) -> CLLocationCoordinate2D {

        let x = p.longitude - 0.0065, y = p.latitude - 0.006;
        let z = sqrt(x * x + y * y) - 0.00002 * sin(y * xPi);
        let theta = atan2(y, x) - 0.000003 * cos(x * xPi);
        return CLLocationCoordinate2D(latitude: z * sin(theta), longitude: z * cos(theta));
    }

    // MARK: - private
    static private func isContains(_ point: CLLocationCoordinate2D, p1: CLLocationCoordinate2D, p2: CLLocationCoordinate2D) -> Bool {
        return (point.latitude >= min(p1.latitude, p2.latitude) && point.latitude <= max(p1.latitude, p2.latitude)) && (point.longitude >= min(p1.longitude, p2.longitude) && point.longitude <= max(p1.longitude, p2.longitude));
    }

    static private func convertLatWithX(_ x: Double, y: Double) -> Double {
        var lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));

        lat += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
        lat += (20.0 * sin(y * pi) + 40.0 * sin(y / 3.0 * pi)) * 2.0 / 3.0;
        lat += (160.0 * sin(y / 12.0 * pi) + 320 * sin(y * pi / 30.0)) * 2.0 / 3.0;
        return lat;
    }

    static private func convertLonWithX(_ x: Double, y: Double) -> Double {
        var lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * sqrt(fabs(x));
        lon += (20.0 * sin(6.0 * x * pi) + 20.0 * sin(2.0 * x * pi)) * 2.0 / 3.0;
        lon += (20.0 * sin(x * pi) + 40.0 * sin(x / 3.0 * pi)) * 2.0 / 3.0;
        lon += (150.0 * sin(x / 12.0 * pi) + 300.0 * sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return lon
    }
}
